package com.example.archermind.myapplication;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Popup;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.RemoteException;
import android.support.annotation.RequiresApi;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.cns.android.onlineservice.OnlineServiceManager;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ExtApiActivity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.btn_addNotification)
    Button btnAddNotification;
    @BindView(R.id.btn_addPopup)
    Button btnAddPopup;
    @BindView(R.id.btn_startForegroundNotificationService)
    Button btnStartForegroundNotificationService;
    @BindView(R.id.tv_Notification)
    TextView tvNotification;
    @BindView(R.id.tv_OnlineService)
    TextView tvOnlineService;
    @BindView(R.id.btn_isOnlineServiceOn)
    Button btnIsOnlineServiceOn;
    @BindView(R.id.btn_getVariant)
    Button btnGetVariant;
    @BindView(R.id.btn_getManufacturer)
    Button btnGetManufacturer;

    private int notificationId = 0x1234;
    private int popupid = 0x4321;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ext_api);
        unbinder = ButterKnife.bind(this);
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    @OnClick(
            {R.id.btn_addNotification, R.id.btn_addPopup, R.id.btn_startForegroundNotificationService,
             R.id.btn_isOnlineServiceOn, R.id.btn_getVariant, R.id.btn_getManufacturer}
    )
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_addNotification:
                addNotification();
                break;
            case R.id.btn_addPopup:
                NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                if (notificationManager != null) {
                    Intent intent = new Intent(this, MainActivity.class);
                    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
                    Popup popup = new Popup("test", "test");
                    popup.setLatitude(30.2);
                    popup.setLongitude(120.4);
                    popup.setActionOne("确定", contentIntent);
                    popup.setActionTwo("导航","nativication");
                    popup.setActionCancel("取消", "cancel");
                    notificationManager.addPopup(popupid++, popup);
                }
                break;
            case R.id.btn_startForegroundNotificationService:
                Intent intent = new Intent(ExtApiActivity.this, NotificationForegroundService.class);
                startService(intent);
                break;
            case R.id.btn_isOnlineServiceOn:
                try {
                    OnlineServiceManager.getInstance(this).isOnlineServiceOn();
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.btn_getVariant:
                try {
                    OnlineServiceManager.getInstance(this).getVariant();
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.btn_getManufacturer:
                try {
                    OnlineServiceManager.getInstance(this).getManufacturer();
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
                break;
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    private void addNotification() {
        notificationId++;
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        NotificationChannel channel = new NotificationChannel("1", "Channel1", NotificationManager.IMPORTANCE_DEFAULT);
        channel.enableLights(true);
        channel.setLightColor(Color.RED);
        channel.setShowBadge(true);
        manager.createNotificationChannel(channel);


        Notification.Builder builder = new Notification.Builder(this, channel.getId());
        builder.setSmallIcon(R.drawable.ic_launcher_foreground)
                .setContentTitle("title")
                .setContentText("text")
                .setNumber(3);
        Intent intent = new Intent(this, MainActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
        builder.setContentIntent(contentIntent);

        manager.notify(notificationId, builder.build());

    }

}
